/* 
 *  <copyright file="ComponentFactory.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;
using System.Collections.Generic;
using SobrietyEngine.Common;

namespace SobrietyEngine.Components
{
    public class ComponentFactory
    {
        static private Dictionary<String, IFactoryBuilder<SobrietyEngine.Components.IComponent>> ByTypeString = null;
        static private Dictionary<String, String> ClassNameToTypeString = null;

        public static void Register(String type, IFactoryBuilder<SobrietyEngine.Components.IComponent> builder)
        {
            if (ByTypeString == null)
                ByTypeString = new Dictionary<String, IFactoryBuilder<IComponent>>();
            else if (ByTypeString.ContainsKey(type))
                Logging.Logger.GetSingleton().WriteError("SE.Actor.ComponentFactory::Register():: component type already exists: " + type);

            String className = type;
            int lind = type.LastIndexOf('.');
            if(lind != -1)
                className = type.Substring(type.LastIndexOf('.') + 1);

            if (ClassNameToTypeString == null)
                ClassNameToTypeString = new Dictionary<string, string>();
            else if (ClassNameToTypeString.ContainsKey(className))
                Logging.Logger.GetSingleton().WriteError("SE.Actor.ComponentFactory::Register():: component with class name already exists: " + className);

            ByTypeString.Add(type, builder);
            ClassNameToTypeString.Add(className, type);
        }

        public static void Register(IFactoryBuilder<SobrietyEngine.Components.IComponent> builder)
        {
            Register(builder.GetType(), builder);
        }

        public static SobrietyEngine.Components.IComponent Build(Type type)
        {
            return Build(type.ToString());
        }

        public static SobrietyEngine.Components.IComponent Build(String type)
        {
            IFactoryBuilder<SobrietyEngine.Components.IComponent> builder;
            if (ByTypeString.TryGetValue(type, out builder))
                return builder.Build();
            Logging.Logger.Debug("SE.Actor.ComponentFactory::Build():: type not found: " + type);

            //we missed whatever we were tryignt o build, lets see if we can find it in our classname list
            String typeStr = TypeStringFromClassName(type);
            if (ByTypeString.TryGetValue(typeStr, out builder))
                return builder.Build();
            Logging.Logger.Error("SE.Actor.ComponentFactory::Build():: type for class name not found: " + typeStr);

            return null;
        }

        public static String TypeStringFromClassName(String className)
        {
            if (ClassNameToTypeString.ContainsKey(className))
                return ClassNameToTypeString[className];
            Logging.Logger.Error("ComponentFactory:: cant find type for class name of " + className);
            return String.Empty;
        }

        public static Boolean DoesExist(string type)
        {
            IFactoryBuilder<SobrietyEngine.Components.IComponent> builder;
            if (ByTypeString.TryGetValue(type, out builder))
                return true;            
            return false;
        }

        public static Dictionary<String, List<ParameterDescription>> GetComponentParameterDescriptions()
        {
            Dictionary<String, List<ParameterDescription>> cp = new Dictionary<string, List<ParameterDescription>>();

            foreach (String compName in ByTypeString.Keys)
            {
                IComponent comp = Build(compName);
                try
                {
                    List<ParameterDescription> cpl = comp.GetParameterDescriptions();
                    cp[compName] = cpl;
                }
                catch (Exception ex)
                {
                    //likely, our component doesnt implement the parameter 
                    if(ex.GetType() == typeof(NotImplementedException))
                    {
                        List<ParameterDescription> cpl = new List<ParameterDescription>();
                        cpl.Add(new ParameterDescription("None","None",typeof(Object), null));
                        cp[compName] = cpl;
                    }
                }
            }

            return cp;
        }
    }
}